package com.open.party.cloud.view

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewBaseFragment
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.MainActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.MsgEntity
import com.open.party.cloud.model.MsgVo
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.msg.MsgDetailActivity
import com.open.party.cloud.view.msg.adapter.MsgMainListAdapter
import com.open.party.cloud.viewModel.MsgViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.views.TitleBarView
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

open class MsgMainFragment : AppRecycleViewBaseFragment<MsgVo>(), OnSuperListener<MsgEntity> {

    private var titleBarView: TitleBarView? = null

    private var viewModel: MsgViewModel? = null
    private var currView: View? = null
    private var adapter: MsgMainListAdapter? = null
    private var parentActivity: MainActivity? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        parentActivity = context as MainActivity
    }

    private fun initTitleView(view: View) {
        titleBarView = view.findViewById(R.id.titleBarView)
        titleBarView!!.setCenterTxt("消息")
        titleBarView!!.setCenterTxtColor(R.color.page_title)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_activity_load_recycle_view_load_tip_shadow, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initTitleView(view)

        viewModel = MsgViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = MsgMainListAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter!!.setOnSuperListener(this)

        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        refreshData()
    }

    fun updateData(orgId: String) {
//        this.orgId = orgId
        refreshData()
    }

    override fun loadData(pageVo: PageVo<MsgVo>) {
        val pageReq = PageReq<MsgVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = 10

        val vo = MsgVo()
        vo.userId = XUtils.string().getNotNullValue(LocalCache.getLoginUserId())
        pageReq.data = vo
        viewModel!!.getMsgPageListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<MsgEntity>>) {

        if ("getMsgPageListData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<MsgEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: MsgEntity, flag: String) {
        XUtils.intent().openActivity(activity, MsgDetailActivity::class.java)
            .putSerializableExtra("notice", itemData)
            .startInFragment(this)
    }
}